﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ReversiBackend;
using System.Diagnostics;
using System.Windows.Forms;

namespace ReversiFrontend
{
    class Human : Player
    {
        private MyMessageBox passBox;
        private bool myTurn = false;
        private bool MyTurn
        {
            get { return myTurn; }
            set
            {
                if (value)
                {
                    myTurn = true;
                    bc.CellClicked += new CellClickedHandler(this.bc_CellClicked);
                }
                else
                {
                    myTurn = false;
                    bc.CellClicked -= new CellClickedHandler(this.bc_CellClicked);
                }
            }
        }
        private BoardControl bc;

        public Human(Game g, int playerIndex, BoardControl bc) : base(g, playerIndex)
        {
            this.bc = bc;
        }

        void bc_CellClicked(Cell cell)
        {
            if (MyTurn)
            {
                if (game.Board.GetValidMoves(this.playerIndex).Count == 0)
                {
                    bool accepted = false;
                    while (!accepted)
                    {
                        //System.Windows.Forms.MessageBox.Show("Kein gültiger Spielzug möglich. Sie müssen passen.");
                        passBox = new MyMessageBox("Keine gültigen Züge möglich. Sie müssen passen", false);
                        passBox.ShowDialog();
                        if (passBox.DialogResult == DialogResult.OK)
                        {
                            accepted = true;
                            passBox.Hide();
                            MyTurn = false;
                            game.Pass();
                        }
                    }
                }
                else
                {

                    if (game.Board.IsValidMove(cell, playerIndex))
                    {
                        MyTurn = false;
                        game.Turn(cell);
                    }
                    else
                    {
                        BoardControl.MarkAsInvalidField(cell);
                        //System.Windows.Forms.MessageBox.Show("Unerlaubter Zug");
                    }

                }
            }
            else
            {
                //     System.Windows.Forms.MessageBox.Show("Sie sind nicht an der Reihe!");
            }
            
        }



        public override string Name
        {
            get { return "Mensch"; }
        }

        public override void YourTurn()
        {
                MyTurn = true;
        }

        public override void Die()
        {
            bc.CellClicked -= new CellClickedHandler(bc_CellClicked);
        }
    }
}
